package com.zwt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 // 开启Swagger2，http://localhost:8080/swagger-ui.html
public class SwaggerConfig {

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    //配置 实例 Docket
    @Bean
    public Docket docket(Environment environment) {

        // 设置要显示swagger的环境
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
//        Profiles profiles = Profiles.of("dev", "test");
//        boolean b = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("李嘉图")
                .enable(true) //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select()// 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.zwt.controller"))
//                .paths(PathSelectors.ant("/controller"))
                .build();
    }

    //配置swagger的信息apiinfo
    private ApiInfo apiInfo() {

        Contact contact = new Contact("李嘉图", "https://www.cnblogs.com/zwtblog/", "1290267280@qq.com");

        return new ApiInfo("李嘉图的接口日记",
                "再小的帆也可以远航",
                "2.2",
                "https://www.cnblogs.com/zwtblog/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
